package com.ktjiaoyu.crm.web.controller;

import com.ktjiaoyu.crm.entity.Chance;
import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.service.IChanceService;
import com.ktjiaoyu.crm.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 * @author Administrator
 */
@Controller
public class SaleController {
    @Resource
    private IChanceService chanceService;

    @Resource
    private IUserService userService;

    @RequestMapping(value = "/chance/list")
    public String list(String chcCustName, String chcTitle, String chcLinkman,
                       Model model, HttpSession session,
                       @RequestParam(required = false, defaultValue = "1") int pageIndex){
        Sort sort = new Sort(Sort.Direction.DESC, "chcCreateDate");
        Pageable pageable = PageRequest.of(pageIndex-1, 5, sort);
        User loginUser = (User) session.getAttribute("loginUser");
        Page<Chance> chancePage = null;

        //判断当前用户是否是客户经理
        Subject subject = SecurityUtils.getSubject();
        if (subject.hasRole("客户经理")){
            chancePage = chanceService.findAllChance(chcCustName, chcTitle, chcLinkman, "0", loginUser.getUsrId(), null, pageable);
        }else {
            chancePage = chanceService.findAllChance(chcCustName, chcTitle, chcLinkman, "0",null, null, pageable);
        }

        model.addAttribute("chcCustName", chcCustName);
        model.addAttribute("chcTitle", chcTitle);
        model.addAttribute("chcLinkman", chcLinkman);
        model.addAttribute("chancePage", chancePage);
        return "chance/list";
    }

    @RequestMapping(value = "/chance/add")
    public String add(Model model){
        return "chance/add";
    }

    @RequestMapping(value = "/chance/addSave")
    public String addSave(Chance chance, HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        chance.setChcCreateId(loginUser.getUsrId());
        chance.setChcCreateBy(loginUser.getUsrName());
        chance.setChcCreateDate(new Date());
        //未分配
        chance.setChcStatus("0");
        this.chanceService.save(chance);
        return "redirect:/chance/list";
    }

    @RequestMapping(value = "/chance/edit")
    public String edit(Model model, Long chcId){
        Chance chance = this.chanceService.getChance(chcId);
        model.addAttribute("chance", chance);
        return "chance/edit";
    }

    @RequestMapping(value = "/chance/editSave")
    public String editSave(Chance chance){
        this.chanceService.edit(chance);
        return "redirect:/chance/list";
    }

    @RequestMapping(value = "/chance/del")
    @ResponseBody
    public Map del(Long chcId, HttpSession session) {
        Map map = new HashMap();
        try {
            Chance chance = chanceService.getChance(chcId);
            User user = (User) session.getAttribute("loginUser");
            if (chance.getChcCreateId().equals(user.getUsrId())){
                this.chanceService.delete(chcId);
                map.put("delResult", "true");
            }else {
                map.put("delResult", "false");
                map.put("msg", "您无权这么做，您不是该销售机会的创建者");
            }
        } catch (Exception e) {
            e.getStackTrace();
            map.put("delResult", "false");
            map.put("msg", "exception:" + e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/chance/appoint")
    public String appoint(Model model, Long chcId){
        Chance chance = this.chanceService.getChance(chcId);
        String chcDueDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        model.addAttribute("chance", chance);
        model.addAttribute("chcDueDate", chcDueDate);
        return "chance/appoint";
    }

    @RequestMapping(value = "/chance/appointSave")
    public String appointSave(Long chcId, Long chcDueId, String chcDueDate) throws ParseException {
        //获得被指派人的信息
        User user = userService.getUser(chcDueId);
        //获得该销售机会的信息
        Chance chance = chanceService.getChance(chcId);
        //修改指派后的信息
        chance.setChcDueId(user.getUsrId());
        chance.setChcDueTo(user.getUsrName());
        chance.setChcDueDate(new SimpleDateFormat("yyyy-MM-dd").parse(chcDueDate));
        chance.setChcStatus("开发中");
        this.chanceService.save(chance);
        return "redirect:/chance/list";
    }

    @RequestMapping(value = "/user/json")
    @ResponseBody
    public List<User> findCustomerManager(Long roleId){
        List<User> list = chanceService.findUsersByRoleId(2L);
        return list;
    }
}
